package com.yohann.exam_service.controller;

import com.yohann.exam_service.entity.Writing;
import com.yohann.exam_service.service.WritingService;
import com.yohann.utils.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 写译表 前端控制器
 * </p>
 *
 * @author Yohann
 * @since 2020-11-19
 */
@Api(tags = "写译")
@RestController
@RequestMapping("/exam_service/writing")
public class WritingController {
    @Autowired
    private WritingService writingService;

    /**
     * 添加写译题目
     *
     * @param writing 题目信息
     * @return 添加状态
     */
    @ApiOperation(("添加写译题目"))
    @PostMapping("addWriting")
    public Result addWriting(@ApiParam("题目信息") @RequestBody Writing writing) {
        writing.setCreateTime(new Date());
        boolean save = writingService.save(writing);

        return Result.state(save);
    }

    /**
     * 删除写译题目
     *
     * @param id 题目id
     * @return 删除状态
     */
    @ApiOperation(("删除写译题目"))
    @GetMapping("deleteWriting/{id}")
    public Result deleteWriting(@ApiParam("题目id") @PathVariable int id) {
        boolean remove = writingService.removeById(id);

        return Result.state(remove);
    }

    /**
     * 查询写译题目
     *
     * @param type 题目类型
     * @return 题目集合
     */
    @ApiOperation(("查询写译题目"))
    @GetMapping("queryWriting/{type}")
    public Result queryWriting(@ApiParam("题目类型") @PathVariable int type) {
        List<Writing> list = writingService.queryWriting(type);

        return Result.ok().data("list", list);
    }

    /**
     * 分页查询写译题目
     *
     * @param current 起始位置
     * @param limit   结束位置
     * @param writing 题目类型
     * @return 结果集
     */
    @ApiOperation(("分页查询写译题目"))
    @PostMapping("query/{current}/{limit}")
    public Result queryWriting(@ApiParam("起始位置") @PathVariable int current,
                               @ApiParam("结束位置") @PathVariable int limit,
                               @ApiParam("查询条件") @RequestBody(required = false) Writing writing) {
        Map<String, Object> map = writingService.queryWriting(current, limit, Objects.isNull(writing) ? 1 : writing.getType());

        return Result.ok().data(map);
    }

    /**
     * 通过id获取写译题目信息
     *
     * @param id 题目id
     * @return 题目信息
     */
    @ApiOperation(("通过id获取写译题目信息"))
    @GetMapping("getWriteById/{id}")
    public Result getWriteById(@ApiParam("题目id") @PathVariable int id) {
        Writing writing = writingService.getById(id);

        return Result.ok().data("item", writing);
    }

    /**
     * 修改写译题目
     *
     * @param writing 题目信息
     * @return 修改状态
     */
    @ApiOperation(("修改写译题目"))
    @PostMapping("updateWriting")
    public Result updateWriting(@ApiParam("题目信息") @RequestBody Writing writing) {
        boolean update = writingService.updateById(writing);

        return Result.state(update);
    }

    /**
     * 翻译
     */
    @ApiOperation("翻译")
    @GetMapping("translate/{str}")
    public Result translate(@ApiParam("翻译信息") @PathVariable("str") String str) {
        String translation = writingService.translate(str);

        return Result.ok().data("translation", translation);
    }

    /**
     * 批改
     */
    @ApiOperation("批改")
    @GetMapping("correct/{str}")
    public Result correct(@ApiParam("批改内容") @PathVariable("str") String str) {
        Map<String, Object> correct = writingService.correct(str);

        return Result.ok().data(correct);
    }
}

